/*
 * Sylvain BERTRAND <digital.ragnarok@gmail.com>
 * code protected by GNU GPL v3
 */
#include <X11/Xlib.h>
#include <glib.h>
#include "geom.h"
#include "window.h"
#include "stacking.h"
#include "misc.h"
#include "client.h"
#include "group.h"
static GHashTable *group_map;
static guint window_hash(Window *w)
{
	return *w;
}

static gboolean window_comp(Window *w1, Window *w2)
{
	return *w1 == *w2;
}

void group_startup(void)
{
	group_map = g_hash_table_new((GHashFunc)window_hash, (GEqualFunc)window_comp);
}

void group_shutdown(void)
{
	g_hash_table_destroy(group_map);
}

struct wm_group *group_add(Window leader, struct wm_client *client)
{
	struct wm_group *self;
	self = g_hash_table_lookup(group_map, &leader);
	if(self == NULL) {
		self = g_slice_new(struct wm_group);
		self->leader = leader;
		self->members = NULL;
		g_hash_table_insert(group_map, &self->leader, self);
	}
	self->members = g_slist_append(self->members, client);
	return self;
}

void group_remove(struct wm_group *self, struct wm_client *client)
{
	self->members = g_slist_remove(self->members, client);
	if(self->members == NULL) {
		g_hash_table_remove(group_map, &self->leader);
		g_slice_free(struct wm_group, self);
	}
}
